const fs = require("fs");
let  join = require('path').join;

const gulp = require('gulp');
const scssJSON  = require("../conf/index").scss;
const scripts  = require("../conf/index").scripts;
const concat = require("gulp-concat");
const connect = require("gulp-connect");
const uglify = require("gulp-uglify");
const cleanCss = require("gulp-clean-css");
var sass = require('gulp-sass');
sass.compiler = require('node-sass');
const babel = require('gulp-babel');
var proxy = require('http-proxy-middleware');


//读取文件路径功能;
function findSync(startPath) {
      let result=[];
      function finder(path) {
          let files=fs.readdirSync(path);
          files.forEach((val,index) => {
              let fPath=join(path,val);
              let stats=fs.statSync(fPath);
              if(stats.isDirectory()) finder(fPath);
              if(stats.isFile()) result.push(fPath);
          });
  
      }
      finder(startPath);
      return result;
}

//监听js改变
function watchScripts(json,prefix){
    for(let attr in json){
        gulp.watch(json[attr].src+'*.js',[prefix + attr]);
    }
}
//监听scss改变
function watchScss(json,prefix){
    for(let attr in json){
          gulp.watch(json[attr].src+"*.scss",[prefix+attr])
    }
}

//分src合并js
function mergeMyScripts(json,prefix='scripts-',mode='dev'){
    let scriptsTaskArray = [];
    for(let attr in json){
        scriptsTaskArray.push(prefix+attr)
        if(mode === 'dev'){
            devScriptsMode(json,prefix,mode,attr)
        }else if(mode === 'build'){
            buildScriptsMode(json,prefix,mode,attr)
        }
    }
    return scriptsTaskArray
}
function devScriptsMode(json,prefix,mode,attr){
    gulp.task(prefix+attr,()=>{
        return  gulp.src([json[attr].src+"*.js"])
        .pipe(concat(attr + '.js'))
        .pipe(gulp.dest('./' + mode +'/scripts'))
        .pipe(connect.reload());
    })
}
function buildScriptsMode(json,prefix,mode,attr){
    gulp.task(prefix+attr,()=>{
        return  gulp.src([json[attr].src+"*.js"])
        .pipe(concat(attr + '.js'))
        .pipe(babel({
            presets: ['@babel/env']
        }))
        .pipe(uglify())
        .pipe(gulp.dest('./dist/scripts'))
    })
}

//分src合并scss
function mergeMyScss(json,prefix='scss-',mode='dev'){
    let scssTaskArray = [];
    for(let attr in json){
        scssTaskArray.push(prefix+attr)
        if(mode === 'dev'){
            devScssMode(json,prefix,mode,attr)
        }else if(mode === 'build'){
            buildScssMode(json,prefix,mode,attr)
        }
    }
    return scssTaskArray;
}

function devScssMode(json,prefix,mode,attr){
    gulp.task(prefix+attr,()=>{
        return  gulp.src([json[attr].src+"*.scss"])
        .pipe(concat(attr + '.scss'))
        .pipe(sass().on('error',sass.logError))
        .pipe(gulp.dest('./' + mode +'/stylesheets'))
        .pipe(connect.reload());
    })
}

function buildScssMode(json,prefix,mode,attr){
    gulp.task(prefix+attr,()=>{
        return  gulp.src([json[attr].src+"*.scss"])
        .pipe(concat(attr + '.scss'))
        .pipe(sass().on('error',sass.logError))
        .pipe(cleanCss({debug: true}, (details) => {
            console.log(`${details.name}: ${details.stats.originalSize}`);
            console.log(`${details.name}: ${details.stats.minifiedSize}`);
          }))
        .pipe(gulp.dest('./dist/stylesheets'))
    })
}

function gpProxy(proxyList){
    let result = [];
    for(let attr in proxyList) {
          let options = {
                target : proxyList[attr].url,
                changeOrigin : true ,
                pathRewrite : !proxyList[attr].rewrite ? {
                      ["^"+attr] : ""
                } : {}
          };
          result.push( proxy( attr , options) )
    }
   return result;
}

module.exports = {
      findSync,
      watchScripts,
      watchScss,
      mergeMyScripts,
      mergeMyScss,
      gpProxy
}